package com.heiyu.task;

import com.google.common.collect.Maps;
import com.heiyu.dao.CoinBaseInfoMapper;
import com.heiyu.dao.OldUserFollowMapper;
import com.heiyu.dao.UserFollowMapper;
import com.heiyu.entity.CoinBaseInfo;
import com.heiyu.entity.OldUserFollow;
import com.heiyu.entity.UserFollow;
import com.heiyu.utils.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

//@Component
public class FollowDataSynTask {

    @Autowired
    private OldUserFollowMapper oldUserFollowMapper;
    @Autowired
    private UserFollowMapper userFollowMapper;
    @Autowired
    private CoinBaseInfoMapper coinBaseInfoMapper;

    @Scheduled(fixedDelay = 100000)
    public void execute(){
        Map<String,Object> map = Maps.newHashMap();
        map = PageHelper.page(oldUserFollowMapper,map,1,100);
        if(map.containsKey("total")){
            Integer total = (Integer) map.get("total");
            if(total>0){
                List<OldUserFollow> list = null;
                UserFollow follow;
                CoinBaseInfo baseInfo;
                List<CoinBaseInfo> coinBaseInfoList;
                for(int i=0;i<total/100;i++){
                    map  = PageHelper.page(oldUserFollowMapper,map,i+1,100);
                    list = (List<OldUserFollow>) map.get("list");
                    for(OldUserFollow oldUserFollow:list){
                        if(StringUtils.isBlank(oldUserFollow.getTCoin())){
                            baseInfo = new CoinBaseInfo();
                            baseInfo.setUsName(oldUserFollow.getFCoin());
                            coinBaseInfoList = null;
                            coinBaseInfoList = this.coinBaseInfoMapper.selectList(baseInfo);
                            if(CollectionUtils.isEmpty(coinBaseInfoList)){
                               continue;
                            }
                            baseInfo = coinBaseInfoList.get(0);
                            follow = new UserFollow();
                            follow.setType("1");
                            follow.setCreateTime(oldUserFollow.getCreateTime());
                            follow.setUserName(oldUserFollow.getUserName());
                            follow.setFCoin(baseInfo.getTargetId());
                            this.userFollowMapper.insert(follow);
                        }
                    }
                }
            }
        }
    }
}
